<p>This rule raises an issue when a traditional C-style for loop is used to iterate over an array when a for-of loop would be more appropriate.</p>
<h2>Why is this an issue?</h2>
<p>Traditional for loops with manual index management are prone to off-by-one errors, which are among the most common bugs in software development.
These errors occur when the loop condition is incorrectly specified (like using <code>⇐</code> instead of <code>&lt;</code>) or when array bounds are
miscalculated.</p>
<p>Modern JavaScript provides the for-of loop syntax, which eliminates the need for manual index management. This approach is:</p>
<ul>
  <li> Safer: No risk of off-by-one errors since there’s no manual index arithmetic </li>
  <li> More readable: The intent to iterate over array elements is clearer </li>
  <li> Less error-prone: No need to remember array.length or worry about loop conditions </li>
</ul>
<p>When you need both the index and the element, you can use <code>array.entries()</code> with destructuring assignment, which still avoids manual
index management while providing both values.</p>
<h3>What is the potential impact?</h3>
<p>Using traditional for loops increases the risk of off-by-one errors that can lead to:</p>
<ul>
  <li> Array index out of bounds errors </li>
  <li> Infinite loops or premature loop termination </li>
  <li> Logic errors that cause incorrect program behavior </li>
  <li> Reduced code maintainability due to more complex loop logic </li>
</ul>
<h3>How to fix?</h3>
<p>Replace the traditional for loop with a for-of loop when you only need the array elements.</p>
<h4>Non-compliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
for (let index = 0; index &lt; array.length; index++) {
    const element = array[index]; // Noncompliant
    console.log(element);
}
</pre>
<h4>Compliant code example</h4>
<pre data-diff-id="1" data-diff-type="compliant">
for (const element of array) {
    console.log(element);
}
</pre>
<h3>Documentation</h3>
<ul>
  <li> <a href="https://github.com/sindresorhus/eslint-plugin-unicorn#readme">eslint-plugin-unicorn</a> - Rule <a
  href="https://github.com/sindresorhus/eslint-plugin-unicorn/blob/HEAD/docs/rules/no-for-loop.md">no-for-loop</a> </li>
  <li> MDN - for…​of statement - <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for…​of">Complete documentation
  of the for-of loop syntax and usage</a> </li>
  <li> MDN - Array.prototype.entries() - <a
  href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries">Documentation for the entries() method used to
  get both index and value</a> </li>
</ul>
<h3>Standards</h3>
<ul>
  <li> ECMAScript 2015 (ES6) - <a href="https://www.ecma-international.org/ecma-262/6.0/#sec-for-in-and-for-of-statements">Official specification for
  for-of loops introduced in ES6</a> </li>
</ul>
